package com.tianyadu.mmall.core.validate.code.controller;

import com.tianyadu.mmall.core.conf.SecurityConstantsConfig;
import com.tianyadu.mmall.core.conf.ValidateCodeProcessorHolder;
import com.tianyadu.mmall.core.validate.code.service.ValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 生成校验码的请求处理器
 *
 * @author xueyj
 * @date 2019-11-05 22:46
 */
@RestController
public class ValidateCodeController {

    /**
     * spring依赖搜索
     * 收集系统中所有的 {@link ValidateCodeProcessor} 接口的实现。
     * 当spring容器启动时，会自动查找ValidateCodeGenerator接口下的实现，将相应接口实现Bean名称放入map中的key
     */
    @Autowired
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;

    /**
     * 创建验证码，根据验证码类型不同，调用不同的
     * {@link com.tianyadu.mmall.core.validate.code.service.ValidateCodeProcessor}接口实现
     *
     * @param request
     * @param response
     * @param type
     * @throws Exception
     */
    @GetMapping(SecurityConstantsConfig.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{type}")
    public void createCode(HttpServletRequest request, HttpServletResponse response, @PathVariable String type)
            throws Exception {
        validateCodeProcessorHolder.findValidateCodeProcessor(type).create(new ServletWebRequest(request, response));
    }

    /**
     * 操作session的工具类
     *//*
    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    *//**
     * 图片验证码生成
     *//*
    @Autowired
    private ValidateCodeGenerator imageCodeGenerator;

    *//**
     * 短信验证码生成
     *//*
    @Autowired
    private ValidateCodeGenerator smsCodeGenerator;

    *//**
     * 短信验证码发送
     *//*
    @Autowired
    private SmsCodeSender smsCodeSender;

    *//**
     * 构建图片验证码信息
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @author xueyj
     * @date 2019/11/5-22:55
     *//*
    @GetMapping("/code/image")
    public void createCode(HttpServletRequest request, @NotNull HttpServletResponse response) throws IOException {
        //  生成图形验证码
        ImageCode imageCode = (ImageCode) imageCodeGenerator.generate(new ServletWebRequest(request));
        //  生成图片格式
        String imageFormatJpeg = SecurityConstantsConfig.IMAGE_FORMAT_JPEG;
        //  验证码放入session时的前缀
        String sessionKey = SecurityConstantsConfig.SESSION_KEY;
        //  将请求信息，sessionKey，value信息添加至session中
        sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, imageCode);
        //  调用图片io流，将图形验证码信息写入HttpServletResponse响应中，io流中写入图片信息，图片格式，写入方式
        boolean write = ImageIO.write(imageCode.getImage(), imageFormatJpeg, response.getOutputStream());
    }

    *//**
     * 构建短信验证码信息
     *
     * @param request
     * @param response
     * @return void
     * @author xueyj
     * @date 2019/11/13-22:02
     *//*
    @GetMapping("/code/sms")
    public void createSmsCode(HttpServletRequest request, @NotNull HttpServletResponse response) throws IOException, ServletRequestBindingException {
        //  生成短信验证码
        ValidateCode smsCode = smsCodeGenerator.generate(new ServletWebRequest(request));
        //  验证码放入session时的前缀
        String sessionKey = SecurityConstantsConfig.SESSION_KEY;
        //  将请求信息，sessionKey，value信息添加至session中
        sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, smsCode);
        String mobile = ServletRequestUtils.getRequiredStringParameter(request, "mobile");
        //  调用短信发送接口，发送短信验证码信息
        smsCodeSender.sendSms(mobile, smsCode.getCode());
    }*/
}